<?php
namespace Winxin\Controller;
class MenuController extends WinxinController{
	
	public function index(){
		$menu = M('menu','wx_')->field('user_id',true)->order('sort desc')->where('user_id = '.session('user_id'))->select();
		$data = self::getMenuTree($menu);
// 		$this->assign('menu',json_encode(array('button'=>$data)));
		$this->assign('menu',$data);
		$this->display();
	}
	
	public function getMenu(){
// 		$menu = json_decode(parent::getMenu());
// 		$buttons = $menu->menu->button;
// 		p($buttons[1]->sub_button[0]);
	}
	private function getMenuTree($menu,$pid = 0){
		$arr = array();
		foreach ($menu as $k=>$v){
			if($v['pid'] == $pid){
				switch($v['type']){
					case 'view':
						unset($v['key']);
						break;
					case 'click':
						unset($v['url']);
						break;
				}
				$v['sub_button'] = self::getMenuTree($menu,$v['id']);
				if(!$v['type']){
					unset($v['type']);
				}
				if(!$v['sub_button']){
					unset($v['sub_button']);
				}
				unset($v['id']);
				unset($v['pid']);
				$arr[] = $v;
			}
		}
		return $arr;
	}
	
}